﻿namespace Mockingbird.MVC.Infrastructure.BaseTypes
{
    using System.Web.Mvc;
    using Core.Infrastructure.Logging.Interfaces;
    using Core.Infrastructure.Membership.Identity.Interface;
    using global::Ninject;
    using Ninject;

    public abstract class BaseController : Controller
    {
        private ILogger logger;

        public BaseController(ILogger logger)
        {
            this.logger = logger;
        }

        protected ILogger Logger
        {
            get { return logger; }
        }

        protected virtual new ICustomPrincipal User
        {
            get { return (HttpContext != null) ? HttpContext.User as ICustomPrincipal : AppKernel.Container.Get<ICustomPrincipal>(); }
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
                base.OnException(filterContext);
            Logger.Error(filterContext.Exception);
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                // If the global handle error filter is enabled, this is not needed
                filterContext.ExceptionHandled = true;

                filterContext.HttpContext.Response.StatusCode = 500;

                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    PartialView("Error").ExecuteResult(ControllerContext);
                }
                else
                {
                    View("Error").ExecuteResult(ControllerContext);
                }
            }
        }
    }
}